package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bocai.yuweiwangluo.adapter.MatchListAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.CityAndTypeBean;
import com.bocai.yuweiwangluo.bean.MatchListBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IMatchListView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class MatchListPresenter
{
    private CityAndTypeBean cityAndTypeBean=new CityAndTypeBean();
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IMatchListView yuezhanListView;
    private Context context;
    private BaseView baseView;
    private int requsttype;
    private MatchListAdapter mMatchListAdapter;
    private MatchListBean matchListBean=new MatchListBean();
    public MatchListPresenter(Context context, IMatchListView yuezhanListView, BaseView baseView)
    {
        this.context = context;
        this.yuezhanListView = yuezhanListView;
        this.baseView=baseView;
    }

    /**
     * 获取约战头部接口
     */
    public void getMatchListHeader()
    {
        try{
            requsttype=1;
            MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
            myOkHttpClient.getYuezhanListHeader(UrlData.BASEURL + UrlData.GETYUEZHANHEADER, handler);

        }catch (Exception e){}
    }
    /**
     * 获取约战列表接口
     */
    public void getMatchList(String page,String type_id,String city_id)
    {
        try
        {
            baseView.showLoading();
            requsttype=2;
            MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
            myOkHttpClient.getYuezhanList(UrlData.BASEURL + UrlData.YUEZHANLIST, page, type_id, city_id, handler);

        }catch (Exception e)
        {}
    }
    public CityAndTypeBean getCityAndTypeBean()
    {

        return cityAndTypeBean;
    }
    public MatchListBean getMatchListbean()
    {

        return matchListBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("result", result);
                        if(requsttype==1)
                        {
                            yuezhanListView.load();
                            baseView.hideLoading();
                            try
                            {
                                cityAndTypeBean = gson.fromJson(result, CityAndTypeBean.class);

                                MyConst.type=cityAndTypeBean.getType();
                                MyConst.citys=cityAndTypeBean.getCity();
                                if(matchListBean.getData().size()==0&yuezhanListView.getmyPage()==1)
                                {
                                    yuezhanListView.getnoContent().setVisibility(View.VISIBLE);
                                    yuezhanListView.getListView().setVisibility(View.GONE);
                                }else
                                {
                                    yuezhanListView.getnoContent().setVisibility(View.GONE);
                                    yuezhanListView.getListView().setVisibility(View.VISIBLE);
                                    if(yuezhanListView.getmyPage()==1)
                                    {
                                        mMatchListAdapter =new MatchListAdapter(context, matchListBean);
                                        yuezhanListView.getListView().setAdapter(mMatchListAdapter);
                                    }else
                                    {
                                        mMatchListAdapter.addData(matchListBean);
                                    }
                                    if(matchListBean.getTotal()<=(matchListBean.getEach()*yuezhanListView.getmyPage()))
                                    {
                                        yuezhanListView.getListView().setPullLoadEnable(false);
                                    }

                                }
                            }catch (Exception e)
                            {

                            }



                        }else
                        {
                            matchListBean = gson.fromJson(result, MatchListBean.class);
                            getMatchListHeader();

                        }
                        break;
                    case RESPONSE_FAILED:
                        yuezhanListView.load();
                        baseView.hideLoading();
                        baseView.showError(result);
                        yuezhanListView.getllRefresh().setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}